﻿Imports System.Windows.Interop
Imports System.Runtime.InteropServices

Imports Stardust.Core
Imports Stardust.Core.Render
<StructLayout(LayoutKind.Sequential)> _
Public Structure MainFrmMARGINS
    Public cxLeftWidth As Integer
    ' width of left border that retains its size 
    Public cxRightWidth As Integer
    ' width of right border that retains its size 
    Public cyTopHeight As Integer
    ' height of top border that retains its size 
    Public cyBottomHeight As Integer
End Structure
Partial Public Class FullScreen

    <DllImport("DwmApi.dll")> _
  Public Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, _
                                                      ByRef pMarInset As MainFrmMARGINS) As Integer
    End Function

    Public Sub UpdateThumbs()
        For Each img As ImageObject In Images
            'Try
            Dispatcher.BeginInvoke(New TAdder(AddressOf AddT), _
                                  Windows.Threading.DispatcherPriority.Background, _
                                  img.OriginalUri)
            'Catch ex As Exception
            'Console.WriteLine(ex.Message)
            'Console.WriteLine(ex.StackTrace)
            'End Try

        Next
    End Sub

    Private Sub FullScreen_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
        ThumbsList.Items.Clear()
        Image1.Source = Nothing
        e.Cancel = True
        Me.Visibility = Windows.Visibility.Hidden
    End Sub

    Private Sub FullScreen_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            Dim mainWindowPtr As IntPtr = New WindowInteropHelper(Me).Handle
            Dim mainWindowSrc As HwndSource = HwndSource.FromHwnd(mainWindowPtr)
            mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0)

            ' Get System Dpi 
            Dim desktop As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(mainWindowPtr)
            Dim DesktopDpiX As Single = desktop.DpiX
            Dim DesktopDpiY As Single = desktop.DpiY

            ' Set Margins 
            Dim margins As New MainFrmMARGINS()

            ' Extend glass frame into client area 
            ' Note that the default desktop Dpi is 96dpi. The margins are 
            ' adjusted for the system Dpi. 
            margins.cxLeftWidth = Convert.ToInt32(5 * (DesktopDpiX / 96))
            margins.cxRightWidth = Convert.ToInt32(5 * (DesktopDpiX / 96))
            margins.cyTopHeight = Convert.ToInt32((CInt(Border1.ActualHeight) + 5) * (DesktopDpiX / 96))
            margins.cyBottomHeight = Convert.ToInt32(5 * (DesktopDpiX / 96))

            Dim hr As Integer = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, margins)
            ' 
            If hr < 0 Then
                'DwmExtendFrameIntoClientArea Failed 
            End If
        Catch generatedExceptionName As DllNotFoundException
            ' If not Vista, paint background white. 
            Application.Current.MainWindow.Background = Brushes.White
        End Try
    End Sub
    Private Function ConvertImage(ByVal src As String, _
                             ByVal PixelHeight As Integer, _
                             ByVal PixelWidth As Integer) _
                             As Windows.Media.Imaging.BitmapImage
        Dim bm1 As New Windows.Media.Imaging.BitmapImage
        bm1.BeginInit()
        Dim uri As New Uri(src, UriKind.RelativeOrAbsolute)
        bm1.UriSource = uri
        If PixelHeight > 0 Then
            bm1.DecodePixelHeight = PixelHeight
        End If
        If PixelWidth > 0 Then
            bm1.DecodePixelHeight = PixelWidth
        End If
        bm1.EndInit()
        Return bm1
    End Function

    Delegate Sub TAdder(ByVal Source As Uri)
    Public Sub AddT(ByVal Source As Uri)
        Dim img As New Image
        img.Width = 30
        img.Height = 30
        img.Stretch = Stretch.Uniform
        Try
            img.Source = ConvertImage(Source.AbsoluteUri, 30, 30)
            ThumbsList.Items.Add(img)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            img.Source = Nothing
        End Try

    End Sub
    Delegate Sub ImageUpdater(ByVal Source As Uri)
    Public Sub UpdateImage(ByVal Source As Uri)
        Try
            Image1.Source = LoadImage(Source)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub ThumbsList_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
        If ThumbsList.SelectedIndex >= 0 Then
            Dispatcher.BeginInvoke(New ImageUpdater(AddressOf UpdateImage), _
                                       Windows.Threading.DispatcherPriority.Background, _
                                       Images(ThumbsList.SelectedIndex).OriginalUri)
        End If

    End Sub
    Public Sub New(ByVal DefaultImage As String)
        InitializeComponent()
        Try
            Dispatcher.BeginInvoke(New ImageUpdater(AddressOf UpdateImage), _
                                   Windows.Threading.DispatcherPriority.Background, _
                                   DefaultImage)

        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub ShowImage(ByVal Image As Uri)
        Try
            'MsgBox(Image)
            Dispatcher.BeginInvoke(New ImageUpdater(AddressOf UpdateImage), _
                                   Windows.Threading.DispatcherPriority.Background, _
                                   Image)

        Catch ex As Exception

        End Try
    End Sub

End Class
